import axios from 'axios';
import qs from 'query-string';

export interface GoodsId {
    id: number;
}

export interface GoodsRecord {
    id: number;
    title: string;
    image: string;
    image_url?: string;
    sort_by: number;
    status?: string;
    created_at: number;
    updated_at: number;
    order_count?: number;
    image_count?: number;
    cat_id?: number;
    sub_cat_id?: number;
    cat_name?: string;
    sub_cat_name?: string;
}

export interface GoodsListRes {
    list: GoodsRecord[];
    total: number;
}

export interface GoodsModel {
    id: number;
    title: string;
    image: string;
    sort_by: number;
}

export interface GoodsParams extends Partial<GoodsRecord> {
    current: number;
    pageSize: number;
    title?: string;
    cat_id?: number;
    sub_cat_id?: number;
}

/**
 * 获取业务列表
 * @param params
 */
export function queryGoodsList(params: GoodsParams) {
    return axios.get<GoodsListRes>('/admin/goods/index', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取业务详情
 * @param params
 */
export function queryGoodsOne(params: GoodsId) {
    return axios.get<GoodsRecord>('/admin/goods/detail', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 提交业务表单（创建/更新）
 * @param data
 */
export function submitGoodsForm(data: any) {
    return axios.post('/admin/goods/submit', data);
}

/**
 * 删除业务
 * @param id
 */
export function delGoods(id: number) {
    return axios.post('/admin/goods/delete', { id });
}

/**
 * 获取商品规格
 * @param goods_id
 */
export function queryGoodsSpecs(goodsId: any) {
    return axios.get('/admin/goods/specs', {
        params: { goods_id: goodsId },
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 添加商品规格
 * @param data
 */
export function addGoodsSpec(data: any) {
    return axios.post('/admin/goods/add-spec', data);
}

/**
 * 添加商品规格值
 * @param data
 */
export function addGoodsSpecValue(data: any) {
    return axios.post('/admin/goods/add-spec-value', data);
}

/**
 * 删除商品规格值
 * @param data
 */
export function delGoodsSpecValue(data: any) {
    return axios.post('/admin/goods/del-spec-value', data);
}

export function queryGoodsKu(goodsId: number) {
    return axios.post('/admin/goods/ku', { goods_id: goodsId });
}

/**
 * 保存SKU数据
 * @param data
 */
export function saveGoodsKu(data: any) {
    return axios.post('/admin/goods/save-ku', data);
}

/**
 * 删除SKU数据
 * @param data
 */
export function delGoodsKu(data: any) {
    return axios.post('/admin/goods/del-ku', data);
}

/**
 * 根据规格组合获取SKU信息
 * @param goods_id 商品ID
 * @param specs 规格组合字符串
 */
export function getSkuBySpecs(goodsId: number, specs: string) {
    return axios.get('/admin/goods/get-sku-by-specs', {
        params: { goods_id: goodsId, specs },
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取商品的所有SKU列表
 * @param goods_id 商品ID
 */
export function getSkuList(goodsId: number) {
    return axios.get('/admin/goods/get-sku-list', {
        params: { goods_id: goodsId },
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 根据SKU编码获取SKU信息
 * @param sku_code SKU编码
 */
export function getSkuByCode(skuCode: string) {
    return axios.get('/admin/goods/get-sku-by-code', {
        params: { sku_code: skuCode },
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

// 相册相关类型定义
export interface GoodsImageRecord {
    id: number;
    title: string;
    path: string;
    url: string;
    sort: number;
    goods_id: number;
    created_at: number;
    updated_at: number;
}

export interface GoodsImageParams {
    goods_id: number;
}

export interface GoodsImageForm {
    goods_id: number;
    path: string;
    title?: string;
    sort?: number;
}

export interface GoodsImageUpdateForm {
    id: number;
    title?: string;
    sort?: number;
    path?: string;
    goods_id?: number;
}

/**
 * 获取商品相册列表
 * @param params
 */
export function queryGoodsImages(params: GoodsImageParams) {
    return axios.get<GoodsImageRecord[]>('/admin/goods/get-images', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 添加商品图片
 * @param data
 */
export function addGoodsImage(data: GoodsImageForm) {
    return axios.post('/admin/goods/add-image', data);
}

/**
 * 更新商品图片信息
 * @param data
 */
export function updateGoodsImage(data: GoodsImageUpdateForm) {
    return axios.post('/admin/goods/update-image', data);
}

/**
 * 删除商品图片
 * @param id
 */
export function delGoodsImage(id: number) {
    return axios.post('/admin/goods/delete-image', { id });
}
